package com.ititer.web.cookie;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class CookieValidator {

	private static String ipRegex = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";

	private String id;
	private String username;
	private int accountType;
	private int accountStatus;
	private String ip;
	private long time;
	private String mobile;
	private long lastLoginTime;
	private int seqId;
	private String nickname;

	public boolean validator(String cookieValue, boolean checkTime, int maxage) {
		//		String decrypto = DESedeEncryptor.decrypt(cookieValue.trim());
		String cookie = cookieValue.trim();
		String[] cookieItems = cookie.split("&");
		if (ArrayUtils.isEmpty(cookieItems)) {
			return false;
		}
		for (String cookieItem : cookieItems) {
			String p = cookieItem;
			if (p.indexOf("=") == -1) {
				continue;
			}
			String[] psa = p.split("=");
			String k = psa[0];
			String v = "";
			if (psa.length == 2) {
				v = psa[1];
			}
			if (Cookier.SEQ_ID.equalsIgnoreCase(k) && v.matches("\\d+")) {
				this.seqId = Integer.parseInt(v);
			} else if (Cookier.ID.equalsIgnoreCase(k)) {
				this.id = v;
			} else if (Cookier.USERNAME.equalsIgnoreCase(k)) {
				this.username = v;
			} else if (Cookier.ACCOUNT_TYPE.equalsIgnoreCase(k) && v.matches("\\d+")) {
				this.accountType = Short.parseShort(v);
			} else if (Cookier.ACCOUNT_STATUS.equalsIgnoreCase(k) && v.matches("\\d+")) {
				this.accountStatus = Short.parseShort(v);
			} else if (Cookier.IP.equalsIgnoreCase(k)) {
				this.ip = v;
			} else if (Cookier.TIME.equalsIgnoreCase(k) && v.matches("\\d+")) {
				this.time = Long.parseLong(v);
			} else if (Cookier.LAST_LOGIN_TIME.equalsIgnoreCase(k)) {
				if (v != null && v.matches("\\d+")) {
					this.setLastLoginTime(Long.parseLong(v));
				}
			} else if (Cookier.NICK_NAME.equalsIgnoreCase(k)) {
				if (v != null) {
					try {
						this.setNickname(URLDecoder.decode(v, Cookier.UTF8));
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
			}
			continue;
		}// end for
		if (StringUtils.isBlank(id)) {
			return false;
		}
		if (StringUtils.isBlank(username)) {
			return false;
		}
		if (this.ip == null || !this.ip.matches(ipRegex)) {
			return false;
		}
		if (this.time <= 0) {
			return false;
		}
		long c = System.currentTimeMillis();
		if (checkTime) {
			if ((this.time + maxage) > c) {
				return false;
			}
		}
		return true;
	}

	public int getAccountStatus() {
		return accountStatus;
	}

	public void setAccountStatus(int accountStatus) {
		this.accountStatus = accountStatus;
	}

	public int getAccountType() {
		return accountType;
	}

	public void setAccountType(int accountType) {
		this.accountType = accountType;
	}

	public String getId() {
		return id;
	}

	public void setGid(String gid) {
		this.id = gid;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMobile() {
		return this.mobile;
	}

	public final long getLastLoginTime() {
		return lastLoginTime;
	}

	public final void setLastLoginTime(long lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public int getSeqId() {
		return seqId;
	}

	public void setSeqId(int seq) {
		this.seqId = seq;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
}
